package org.ax.service.sys.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.ax.mapper.sys.PermissionMapper;
import org.ax.mapper.sys.RoleMapper;
import org.ax.pojo.sys.Permission;
import org.ax.pojo.sys.Role;
import org.ax.service.sys.PermissionService;
import org.ax.service.sys.RolePermissionService;
import org.ax.service.sys.RoleService;
import org.ax.service.sys.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-23-14:49
 */
@Service
public class IPermissionService extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RolePermissionService rolePermissionService;

    @Override
    public List<Permission> toUserIdFindPerm(String userId) {
        String roleId = userRoleService.toUserIdFindRole(userId);
        String permId = rolePermissionService.toRoleIdFindPermissionId(roleId);
        String[] permIds = permId.split(",");
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<Permission>();
        queryWrapper.in("id", permIds);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public Map<Permission, List<Permission>> permissionAll() {
        Map<Permission, List<Permission>> permissionListMap=new TreeMap<Permission, List<Permission>>((o1, o2) -> o1.getId()-o2.getId());
        QueryWrapper<Permission> wrapper=new QueryWrapper<Permission>();
        Subject subject = SecurityUtils.getSubject();
        wrapper.eq("parentId", "0");
        List<Permission> permissionList = getBaseMapper().selectList(wrapper);
        for (Permission permission : permissionList) {
            if(subject.isPermitted(permission.getPercode())){
                QueryWrapper<Permission> wrapper2=new QueryWrapper<Permission>();
                wrapper2.eq("parentId", permission.getId());
                wrapper2.orderByAsc("id");
                List<Permission> permissions = getBaseMapper().selectList(wrapper2);
//                permissions.
//         permissions.put(permission, permissionList.stream().filter(permission1 -> permission1.getParentId()==permission.getId()).collect(Collectors.toList()));

                permissionListMap.put(permission,permissions.stream().filter(permission1 -> subject.isPermitted(permission1.getPercode())).collect(Collectors.toList()));
            }


        }
        return permissionListMap;
    }

    @Override
    public Map<Permission, List<Permission>> permissions() {
        Map<Permission, List<Permission>> permissionListMap=new TreeMap<Permission, List<Permission>>((o1, o2) -> o1.getId()-o2.getId());
        QueryWrapper<Permission> wrapper=new QueryWrapper<Permission>();
        wrapper.eq("parentId", "0");
        List<Permission> permissionList = getBaseMapper().selectList(wrapper);
        for (Permission permission : permissionList) {
            QueryWrapper<Permission> wrapper2=new QueryWrapper<Permission>();
            wrapper2.eq("parentId", permission.getId());
            wrapper2.orderByAsc("id");
            List<Permission> permissions = getBaseMapper().selectList(wrapper2);
            permissionListMap.put(permission,permissions);
        }
        return permissionListMap;
    }


//    @Override
//    public Map<Permission, List<Permission>> permissionAll() {
//        Map<Permission, List<Permission>> permissionListMap=new HashMap<Permission, List<Permission>>();
//        QueryWrapper<Permission> wrapper=new QueryWrapper<Permission>();
//        wrapper.eq("type", "menu");
//        List<Permission> permissionList = getBaseMapper().selectList(wrapper);
//        for (Permission permission : permissionList) {
//            QueryWrapper<Permission> wrapper2=new QueryWrapper<Permission>();
//            wrapper2.eq("parentId", permission.getId());
//            permissionListMap.put(permission,getBaseMapper().selectList(wrapper2));
//
//        }
//        return permissionListMap;
//    }
}
